#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>

void print_int(int arr[], int sz)
{
	int i = 0;
	for (i = 0;i < sz;i++)
	{
		printf("%d", arr[i]);
	}
}
int cmp_int(const void* p1, const void* p2)
{
	return (*(int*)p1 - *(int*)p2);
}
void swap(char* BU1, char* BU2, int size)
{
	int i = 0;
	char tmp = 0;
	for (i = 0;i < size - 1;i++)
	{
		tmp = *BU1;
		*BU1 = *BU2;
		*BU2 = tmp;
		BU1++;
		BU2++;
	}
}
void bubble_sort(void* base, int sz, int size, void (*cmp)(const void* p1, const void* p2))
{
	int i = 0;
	for (i = 0;i < sz-1;i++)
	{
		int j = 0;
		for (j = 0;j < sz - 1 - i;j++)
		{
			if (cmp_int((char*)base + j * size, (char*)base + (j + 1) * size) > 0)
			{
				swap((char*)base + j * size, (char*)base + (j + 1) * size, size);
				
			}
		}
	}
}
void test1()
{
	int arr[10] = { 3,4,2,1,6,7,5,8,9,10 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	bubble_sort(arr, sz, sizeof(arr[0]), cmp_int);
	print_int(arr, sz);
}
struct Stu 
{
	char name[20];
	int age;
};
void print_age(int sz, struct Stu* p)
{
	int i = 0;
	while (1)
	{
		printf("%s%d ", (p + i)->name, (p + i)->age);
		i++;
		if (i == sz)
		{
			break;
		}
	}
}
int cmp_age(const void* p1, const void* p2)
{
	return ((struct Stu*)p1)->age - ((struct Stu*)p2)->age;
}
void test2()
{
	struct Stu aee[3] = { {"zhu",19}, {"xu",18},{"niu",20} };
	int sz = sizeof(aee) / sizeof(aee[0]);
	bubble_sort(aee, sz, sizeof(aee[0]), cmp_age);
	print_age(sz, aee);
}
int cmp_name(const void* p1, const void* p2)
{
	return strcmp(((struct Stu*)p1)->name , ((struct Stu*)p2)->name);
}
void test3()
{
	struct Stu aee1[3] = { {"z",19}, {"x",18},{"n",20} };
	int sz = sizeof(aee1) / sizeof(aee1[0]);
	bubble_sort(aee1, sz, sizeof(aee1[0]), cmp_name);
	print_age(sz, aee1);
}
int main()
{
	test1();
	printf("\n");
	test2();
	printf("\n");
	test3();
	return 0;
}